package xyz.naokeziteng.leetcode;

//Definition for singly-linked list.
public class ListNode {
    int val;
    public ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public ListNode(int[] arr) {
        if (arr.length == 0) {
            throw new IllegalArgumentException("Create failed, arr length cannot be zero.");
        }
        this.val = arr[0];
        if (arr.length == 1) {
            return;
        }
        ListNode cur = this;
        for (int i = 1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("linked list: ");
        sb.append(val);
        ListNode cur = this;
        while (cur.next != null) {
            sb.append(" --> ");
            sb.append(cur.next.val);
            cur = cur.next;
        }
        return sb.toString();
    }
}
